package com.tenebrous.jestic.service.framwork.meber.author;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthGroup;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthGroupUser;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.mapper.idao.framwork.meber.author.AuthGroupMapper;
import com.tenebrous.jestic.mapper.idao.framwork.meber.author.AuthGroupUserMapper;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AuthGroupService extends SuperService<AuthGroup> {

    @Autowired
    private AuthGroupMapper mapper;

    @Autowired
    private AuthGroupUserMapper groupuserMapper;

    @Override
    @Autowired
    protected void setSuperMapper() {

        super.setSuperMapper(this.mapper);
    }

    public PageInfo<AuthGroup> queryPagingList(Integer pageIndex, Integer pageSize, String categoryid, String name) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(categoryid, name));
    }

    public PageInfo<OrgUser> queryPagingGroupUserList(Integer pageIndex, Integer pageSize, String groupid, String realname) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.queryGroupUserList(groupid, realname));
    }

    public List<OrgUser> queryGroupUserList(String groupid) {
        return this.queryGroupUserList(groupid, null);
    }

    private List<OrgUser> queryGroupUserList(String groupid, String realname) {
        return this.mapper.selectUserListByGroupid(groupid, realname);
    }

    @Transactional
    public Object insertGroupUsers(String groupid, String[] userids) {
        int i = 0;
        Date dt = new Date();
        for (String userid : userids) {
            AuthGroupUser item = new AuthGroupUser();
            item.setGroupid(groupid);
            item.setUserid(userid);
            if (this.groupuserMapper.selectCount(item) == 0) {
                item.setId(UUIDUtils.randomUUID());
                item.setCreateDate(dt);
                this.groupuserMapper.insertSelective(item);
                i++;
            }
        }
        return i;
    }

    @Transactional
    public int deleteById(String groupid) {
        this.mapper.deleteById(groupid);
        return 1;
    }

    @Transactional
    public int deleteByIds(String[] groupids) {
        int i = 0;
        for (String id : groupids) {
            if (StringUtils.isNotBlank(id)) {
                this.mapper.deleteById(id);
                i++;
            }
        }
        return i;
    }

    @Transactional
    public int deleteGroupUsers(String groupid, String[] userids) {
        int i = 0;
        for (String userid : userids) {
            this.mapper.deleteGroupUserByCondition(groupid, userid);
            i++;
        }
        return i;
    }
}
